<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门:" label-width="140px" prop="deptId">
        <el-tree-select
          v-model="queryParams.deptId"
          :data="deptOptions"
          :props="{ value: 'id', label: 'label', children: 'children' }"
          value-key="id"
          placeholder="请选择部门"
          check-strictly
        />
      </el-form-item>
      <el-form-item label="门闸类型:" label-width="140px" prop="doorType">
        <el-select v-model="queryParams.doorType" placeholder="请选择门闸类型">
          <el-option
            v-for="dict in doorTypeOption"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="进出类型:" label-width="140px" prop="doorStart">
        <el-select v-model="queryParams.doorStart" placeholder="请选择门闸进出类型">
          <el-option
            v-for="dict in doorEventOption"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>
    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="门闸类型" prop="doorType">
        <template #default="scope">
          <span>{{ scope.row.doorType == "1" ? "门禁" : "道闸" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门名称" width="180" prop="deptName" />
      <el-table-column label="编号" width="140" prop="doorCode" />
      <el-table-column label="名称" width="180" prop="doorName" />
      <el-table-column label="进出类型" width="180" prop="doorStart">
        <template #default="scope">
          <span>{{ scope.row.doorStart == 1 ? "出" : "进" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="门闸详细位置"
        prop="doorDesc"
        width="350"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="经度" prop="latitude" />
      <el-table-column label="纬度" prop="longitude" />

      <el-table-column label="IP" prop="doorIp" :show-overflow-tooltip="true" />

      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
    <!--新增或者修改门闸设备对话框-->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="门闸类型:" label-width="140px" prop="doorType">
              <el-select v-model="form.doorType" placeholder="请选择门闸类型">
                <el-option
                  v-for="dict in doorTypeOption"
                  :key="dict.id"
                  :label="dict.label"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进出类型:" label-width="140px" prop="doorStart">
              <el-select v-model="form.doorStart" placeholder="请选择门闸进出类型">
                <el-option
                  v-for="dict in doorEventOption"
                  :key="dict.id"
                  :label="dict.label"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="编号:" label-width="140px" prop="doorCode">
          <el-input v-model="form.doorCode" placeholder="请输入门闸编号" />
        </el-form-item>
        <el-form-item label="名称:" label-width="140px" prop="doorName">
          <el-input v-model="form.doorName" placeholder="请输入门闸名称" />
        </el-form-item>
        <el-form-item label="详细位置:" label-width="140px" prop="doorDesc">
          <el-input
            type="textarea"
            :row="3"
            v-model="form.doorDesc"
            placeholder="请输入门闸详细位置描述"
          />
        </el-form-item>
        <el-form-item label="所在部门:" label-width="140px" prop="deptId">
          <el-tree-select
            v-model="form.deptId"
            :data="deptOptions"
            :props="{ value: 'id', label: 'label', children: 'children' }"
            value-key="id"
            placeholder="请选择部门"
            check-strictly
          />
        </el-form-item>

        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="所在位置经度:" label-width="140px" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入门闸所在位置经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在位置纬度:" label-width="140px" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入门闸所在位置纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="三维地图坐标:" label-width="140px" prop="coords">
          <el-input v-model="form.coords" placeholder="请输入设备所在三维地图坐标" />
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="账号:" label-width="140px" prop="doorAdmin">
              <el-input v-model="form.minimumRange" placeholder="请输入门闸账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码:" label-width="140px" prop="doorPws">
              <el-input v-model="form.maximumRange" placeholder="请输入门闸密码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="IP:" label-width="140px" prop="doorIp">
          <el-input v-model="form.doorIp" placeholder="请输入门闸ip" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="ScreenDoor">
import { listDoor, addDoor, deleteDoor, updateDoor, getDoor } from "@/api/screen/door";
import { deptTreeSelect } from "@/api/system/user";
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const title = ref("");
const open = ref(false);
const { proxy } = getCurrentInstance();
const deptOptions = ref([]);
const doorTypeOption = ref([
  { id: "1", label: "门禁" },
  { id: "2", label: "道闸" },
]);
const doorEventOption = ref([
  { id: "0", label: "进门" },
  { id: "1", label: "出门" },
]);

const data = reactive({
  form: {
    typeId: undefined,
  },
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
  },
  rules: {
    doorName: [
      {
        required: true,
        message: "门闸名称必须填写！",
        trigger: ["blur", "change"],
      },
      {
        min: 3,
        max: 100,
        message: "名称必须介于 3 和 100 之间",
        trigger: "blur",
      },
    ],
    doorCode: [
      {
        required: true,
        message: "门闸编号必须填写！",
        trigger: ["blur", "change"],
      },
      {
        min: 3,
        max: 100,
        message: "编号必须介于 3 和 100 之间",
        trigger: "blur",
      },
    ],
    doorType: [{ required: true, message: "门闸类型必须选择！", trigger: "blur" }],
    doorStart: [{ required: true, message: "进出类型必须选择！", trigger: "blur" }],
    deptId: [{ required: true, message: "部门必须选择！", trigger: "blur" }],
    units: [{ required: true, message: "参数单位必须填写！", trigger: "blur" }],
    latitude: [{ required: true, message: "经度必须填写！", trigger: "blur" }],
    longitude: [{ required: true, message: "纬度必须填写！", trigger: "blur" }],
  },
});
const { queryParams, form, rules } = toRefs(data);
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

/** 获取列表数据 */
function getList() {
  loading.value = true;
  listDoor(queryParams.value).then((response) => {
    loading.value = false;
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
  });
}
/** 重置 */
function reset() {
  form.value = {};
  proxy.resetForm("formRef");
}

/** 处理新增 */
function handleAdd() {
  open.value = true;
  title.value = "新增门闸数据";
  reset();
}

/** 处理删除 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.doorName + '"的数据项?')
    .then(function () {
      return deleteDoor(row.doorId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 处理编辑 */
function handleUpdate(row) {
  getDoor(row.doorId).then((response) => {
    open.value = true;
    title.value = "编辑门闸数据";
    form.value = response.data;
    form.value.deptId = parseInt(response.data.deptId);
  });
}
/** 处理取消 */
function cancel() {
  open.value = false;
}

/** 处理数据提交 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.doorId != undefined) {
        updateDoor(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改门闸成功");
          open.value = false;
          getList();
        });
      } else {
        addDoor(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增门闸成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

getList();
getDeptTree();
</script>
